package com.changgou.pay.service.impl;

import com.changgou.common.util.HttpClient;
import com.changgou.pay.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import org.bouncycastle.util.IPAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeixinPayServiceImpl implements WeixinPayService {

    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.partner}")
    private String partner;

    @Value("${weixin.partnerkey}")
    private String partnerkey;

    @Value("${weixin.notifyurl}")
    private String notifyurl;

    /**
     * 创建支付二维码链接
     * @param requestParamMap 请求参数；包含内容如下：
        *                        outTradeNo 订单号
        *                        money 支付金额
     * @return
     */
    @Override
    public Map<String, String> createNative(Map<String, String> requestParamMap) {

        Map<String,String> returnMap=new HashMap<>();

        try {
            //1、组合要发送到微信支付的参数
            Map<String,String> paramMap=new HashMap<>();

            //从微信申请的公众账号ID
            paramMap.put("appid",appid);

            //从微信申请的商户号
            paramMap.put("mch_id",partner);

            //随机字符串
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());

            //微信sdk提供有工具类包生成
            //paramMap.put("sign","");

            //商品描述-可以设置为商品的标题
            paramMap.put("body","畅购商城");

            //订单号
            paramMap.put("out_trade_no",requestParamMap.get("outTradeNo"));

            //交易总金额
            paramMap.put("total_fee",requestParamMap.get("money"));

            //当前机器ip
            paramMap.put("spbill_create_ip", "127.0.0.1");

            //回调地址
            paramMap.put("notify_url",notifyurl);

            //交易类型：扫描支付
            paramMap.put("trade_type","NATIVE");

            //2、将参数map转换为微信支付需要的xml
            String signedXml = WXPayUtil.generateSignedXml(paramMap, partnerkey);
            System.out.println("发送到微信统一下单的参数为：" + signedXml);

            //3、创建httpCient对象并发送信息到微信支付
            HttpClient httpClient=new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            httpClient.setHttps(true);
            httpClient.setXmlParam(signedXml);
            httpClient.post();

            //4、获取微信支付返回的数据
            String content = httpClient.getContent();
            System.out.println("微信统一下单返回的内容为：" + content);

            //5、转换内容为map并设置返回结果
            Map<String, String> resultMap = WXPayUtil.xmlToMap(content);

            //业务结果

            returnMap.put("result_code",resultMap.get("result_code"));
            //二维码链接地址
            returnMap.put("code_url",resultMap.get("code_url"));
            //订单号
            returnMap.put("out_trade_no",requestParamMap.get("out_trade_no"));
            //支付金额
            returnMap.put("total_fee",requestParamMap.get("money"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnMap;
    }

    /**
     * 根据订单号查询订单
     * @param outTradeNo:订单号
     * @return
     */
    @Override
    public Map<String, String> querystatus(String outTradeNo) {
        Map<String,String> returnMap=new HashMap<>();

        //1、组合要发送的参数
        try {
            Map<String,String> paramMap=new HashMap<>();

            //从微信申请的公众账号ID
            paramMap.put("appid",appid);


            //从微信申请的商户号
            paramMap.put("mch_id",partner);

            //随机字符串
            paramMap.put("nonce_str",WXPayUtil.generateNonceStr());

            //微信sdk提供有工具类包生成
            //paramMap.put("sign","");

            //商户订单号
            paramMap.put("out_trade_no",outTradeNo);

            //2、将参数map转换为微信支付需要的xml
            String signedXml = WXPayUtil.generateSignedXml(paramMap, partnerkey);
            System.out.println("发送到微信支付查看订单的内容为：" + signedXml);

            //3、创建httpCient对象并发送信息到微信支付
            HttpClient httpClient=new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setHttps(true);
            httpClient.setXmlParam(signedXml);
            httpClient.post();

            //4、获取微信支付返回的数据
            String content = httpClient.getContent();
            System.out.println("微信查询订单返回的内容为：" + content);

            //5、转换内容为map并设置返回结果
            Map<String, String> toMap = WXPayUtil.xmlToMap(content);

            return WXPayUtil.xmlToMap(content);


        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }
}
